package com.longdaji.decoration.ui.activitiesOfMine.accountManagement.changePassword;

import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.StringUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/1/23 0023.
 */

public class ChangePasswordPresenter extends RxPresenter<ChangePasswordContract.View> implements ChangePasswordContract.Presenter {
    private DataManager mDataManager;

    @Inject
    public ChangePasswordPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void onConfirmClick(String newPassword, String newPasswordAgain) {
        Account account = Account.getInstance();
        if (StringUtil.isBlank(newPassword)) {
            mView.showNewPasswordBlank();
        }
        if (!TextUtils.equals(newPassword, newPasswordAgain)) {
            mView.showPasswordDifferent();
            return;
        }
        doUpdatePassword(newPassword);
    }

    private void doUpdatePassword(final String newPassword) {
        addSubscribe(mDataManager.doUpdateAccountApi(Account.getInstance().getUserid(), null, null, newPassword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d("ChangePasswordPresenter","succeed : doUpdatePassword ");
                        Account.getInstance().setPassword(newPassword);
                        mView.showUpdatePasswordSucceed();
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("ChangePasswordPresenter","failed : " + msg);
                    }
                }));
    }
}
